//
//  AppDelegate.swift
//  SinaWebo
//
//  Created by 亚杏 on 2017/5/5.
//  Copyright © 2017年 oms. All rights reserved.
//

import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        //如果是10.0以上版本
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.carPlay,.sound]) { (success, error) in
                print("授权" + (success ? "成功": "失败"))
            }
        } else {
            //10.0以下
            //取得用户授权显示通知（上方提示条、声音、BadgeNumber）
            let notiSetting = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
            application.registerUserNotificationSettings(notiSetting)
        }

        window = UIWindow()
        window?.backgroundColor = UIColor.white
        window?.rootViewController = WBMainViewController()

        loadAppInfo()

        window?.makeKeyAndVisible()

        return true
    }

    //MARK:从服务器加载应用程序信息，网络的tabbaritem信息
    private func loadAppInfo() {

        //1.模拟异步
        DispatchQueue.global().async {
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
            let data = NSData(contentsOf: url!)
            //取沙盒目录
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonPath = (docDir as NSString).appendingPathComponent("main.json")
            data?.write(toFile:jsonPath, atomically: true)
            print("\(jsonPath)")
        }
    }

}



